import bpy 
import os
import json 
import copy


__JSONCONFIG_FILE = '''
{
    "outpath":"/Users/mac/data0/private_work/cocoscreator_proj/client_proj/cleaner_baby/assets/resources/mesh"
}
'''

def load_export_conifg():
    dir_model = os.path.dirname(os.path.dirname(__file__))
    path_json = os.path.join(dir_model, __JSONCONFIG_FILE)
    print(path_json)
    return json.loads(__JSONCONFIG_FILE)


def run(outpath):   
    basedir = os.path.dirname(bpy.data.filepath)

    view_cur = bpy.context.view_layer
    obj_active = view_cur.objects.active
    objs = bpy.context.selected_objects

    if len(objs) > 0:
        obj = objs[0]
        old_location = copy.copy(obj.location)
        obj.location = [0,0,0]
        name = bpy.path.clean_name(obj.name)
        outfile = os.path.join(outpath, name+".glb")
        print("written:", outfile)
        if os.path.isfile(outfile):
            os.remove(outfile)
        bpy.ops.export_scene.gltf(filepath=outfile, use_selection=True)
        print("suc!")
        obj.location =  old_location

if __name__ == '__main__':
    conf = load_export_conifg()
    print("outpath is ", conf["outpath"])
    run(conf["outpath   